// pages/goodsSale/index.js

import {
  GoodsSaleModel
} from '../../models/goodsSale.js'
let goodsSaleModel = new GoodsSaleModel()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    datas:[],
    count: 0,
    page:1,
    pagesize:22,
    start:'',
    end:'',
    isLoading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initTime()
    wx.showLoading({
      title: '玩命加载中',
    })
    this.loadData(this.data.start, this.data.end, this.data.page, this.data.pagesize)
  },

  initTime(){
    // var date1 = new Date(new Date(new Date().toLocaleDateString()).getTime() - 24 * 60 * 60 * 1000);
    // var date2 = new Date(new Date(new Date().toLocaleDateString()).getTime());

    var date1 = new Date(new Date(new Date().toLocaleDateString()).getTime());
    var date2 = new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1);
    var start = Date.parse(date1) / 1000;
    var end = Date.parse(date2) / 1000;
    console.log(start)
    console.log(end)
    this.setData({
      start:start,
      end:end,
      count:0,
      datas: [],
      page:1
    })
  },

  loadData(start,end,page,pagesize){
    
    const listInfo = goodsSaleModel.goodsSale(start, end, page, pagesize, (data) => {
      wx.hideLoading();
      // console.log(data)
      if (data.code === 1 && data.data.length > 0) {
        this.setData({
          datas: this.data.datas.concat(data.data),
          count: this.data.count + data.data.length,
          isLoading: false
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("pull up")
    this.setData({
      page:this.data.page+1,
      isLoading: true
    })
    // console.log(this.data.isLoading)
    this.loadData(this.data.start, this.data.end, this.data.page, this.data.pagesize)
    this.setData({
      isLoading: false
    })
    // console.log(this.data.isLoading)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})